﻿// MyNotes - Free Sticky Notes
// Copyright (C) 2011 Alexandre Vicenzi
// 
// This program is free software; you can redistribute it and/or modify it under 
// the terms of the GNU General Public License as published by the Free Software 
// Foundation; either version 3 of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT 
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with 
// this program; if not, write to the Free Software Foundation, Inc., 
// 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyNotes
{
    public partial class FrmNotes : Form
    {
        private int mouseLeft;
        private int mouseTop;
        private RichTextBox rtbText = null;
        private WebBrowser wbNote = null;
        private PictureBox imageNote = null;
        private Annotation annotation = null;
        private bool changed = false;

        public FrmNotes()
        {
            InitializeComponent();
            toolTip.SetToolTip(btnAdd, "Add new annotation");
            toolTip.SetToolTip(btnClose, "Delete annotation");
        }

        // Funções de manipulação das configurações.
        #region Class functions

        // Chamar sempre após a criação da tela.
        public void SetAnnotation(Object note)
        {
            this.annotation = (Annotation)note;
            LoadParameters();
        }

        // Carrega os parametros da tela a partir da anotacão.
        public void LoadParameters()
        {
            switch (this.annotation.Color)
            {
                case NoteColor.Yellow:
                    miYellow.PerformClick();
                    break;
                case NoteColor.Green:
                    miGreen.PerformClick();
                    break;
                case NoteColor.Purple:
                    miPurple.PerformClick();
                    break;
            }

            switch (this.annotation.Type)
            {
                case AnnotationType.Image:
                    miImageType.PerformClick();
                    LoadImage(this.annotation.Text);
                    break;
                case AnnotationType.Text:
                    miTextType.PerformClick();
                    this.rtbText.Text = this.annotation.Text;
                    break;
                case AnnotationType.Web:
                    miWebType.PerformClick();
                    if (!String.IsNullOrEmpty(this.annotation.Text))
                        this.wbNote.Navigate(this.annotation.Text);
                    break;
            }

            this.Size = this.annotation.FormSize;
            if (this.annotation.Locked)
                miLockControl.PerformClick();
            if (this.annotation.Location.X != 0 && this.annotation.Location.Y != 0)
                this.Location = this.annotation.Location;
        }

        // Adiciona um RichTextBox.
        public void AddRichText()
        {
            if (this.rtbText == null)
            {
                RemoveControls();
                this.rtbText = new RichTextBox();
                this.rtbText.BackColor = this.pnText.BackColor; // Color.FromArgb(255, 255, 192);
                this.rtbText.BorderStyle = BorderStyle.None;
                this.rtbText.ContextMenuStrip = this.cmText;
                this.rtbText.Font = new Font("Segoe Print", 11.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
                this.rtbText.Location = new Point(4, 4);
                //this.rtbText.Name = "rtbText";
                this.rtbText.RightToLeft = RightToLeft.No;
                this.rtbText.ScrollBars = RichTextBoxScrollBars.Vertical;
                //this.rtbText.Size = new System.Drawing.Size(56, 60);
                //this.rtbText.TabIndex = 0;
                //this.rtbText.Text = "";
                this.rtbText.Dock = DockStyle.Fill;
                this.pnText.Controls.Add(this.rtbText);
            }
        }

        // Adiciona um ImageBox.
        public void AddImageBox()
        {
            if (this.imageNote == null)
            {
                RemoveControls();
                this.imageNote = new PictureBox();
                this.imageNote.Location = new Point(4, 4);
                this.ContextMenuStrip = cmImage;
                this.imageNote.TabStop = false;
                this.imageNote.SizeMode = PictureBoxSizeMode.AutoSize;
                this.imageNote.Dock = DockStyle.Fill;
                this.pnText.Controls.Add(this.imageNote);
            }
        }

        // Carrega uma imagem para o ImageBox.
        public void LoadImage(string fileName)
        {
            if ((this.imageNote != null) && (!String.IsNullOrEmpty(fileName)))
            {
                try
                {
                    Image image = new Bitmap(fileName);
                    this.imageNote.Image = image;
                    this.annotation.Text = fileName;
                }
                catch //(Exception ex)
                {
                    this.imageNote.Image = this.imageNote.ErrorImage;
                }
            }
        }

        // Adiciona um WebBrowser.
        public void AddWebBrowser()
        {
            if (this.wbNote == null)
            {
                RemoveControls();
                this.wbNote = new WebBrowser();
                this.wbNote.Location = new Point(4, 4);
                this.ContextMenuStrip = cmBrowser;
                this.wbNote.Dock = DockStyle.Fill;
                this.pnText.Controls.Add(this.wbNote);
            }
        }

        // Remove os controles ativos.
        private void RemoveControls()
        {
            this.pnText.Controls.Clear();
            this.wbNote = null;
            this.imageNote = null;
            this.rtbText = null;
        }

        // Altera a cor.
        public void SwapColor(NoteColor cn)
        {
            SetColor(AnnotationColor.GetBaseColor(cn), AnnotationColor.GetTopColor(cn));
            this.annotation.Color = cn;
        }

        private void SetColor(Color colorBase, Color colorPanel)
        {
            // Se o controle ativo for o TextBox atribui a cor para ele também.
            if (this.rtbText != null)
                this.rtbText.BackColor = colorBase;

            this.pnText.BackColor = colorBase;
            this.btnAdd.BackColor = colorPanel;
            this.btnClose.BackColor = colorPanel;
            this.pnTop.BackColor = colorPanel;
        }

        #endregion

        // Eventos da form.
        #region Events Form

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Do you want delete this annotation?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                Notes.RemoveAnnotation(this.annotation);
            }
        }

        private void pnTop_MouseDown(object sender, MouseEventArgs e)
        {
            mouseLeft = e.Location.X;
            mouseTop = e.Location.Y;
        }

        // Move a form ao arrastar o panel superior.
        private void pnTop_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && (!this.annotation.Locked))
            {
                this.Left = this.Left + e.Location.X - mouseLeft;
                this.Top = this.Top + e.Location.Y - mouseTop;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // Tira o foco do botão.
            this.pnText.Focus();
            Annotation note = new Annotation(AnnotationType.Text, this.annotation.Color);
        }

        private bool MouseInPanel(MouseEventArgs e)
        {
            if (e.Location.Y > (Height - 10) &&
                e.Location.X > (Width - 10))
                return true;
            else
                return false;
        }

        private void pnText_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.X > mouseLeft)
                {
                    this.Height = this.Height + e.Y - mouseTop;
                    mouseTop = e.Y;
                }
                else
                {
                    this.Height = this.Height - (e.Y - mouseTop) * -1;
                    mouseTop = e.Y;
                }

                if (e.X < mouseLeft)
                {
                    this.Width = this.Width - (e.X - mouseLeft) * -1;
                    mouseLeft = e.X;
                }
                else
                {
                    this.Width = this.Width + e.X - mouseLeft;
                    mouseLeft = e.X;
                }
            }
        }

        private void pnText_MouseDown(object sender, MouseEventArgs e)
        {
            mouseLeft = e.Location.X;
            mouseTop = e.Location.Y;
        }

        private void FrmNotes_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseInPanel(e))
                this.Cursor = Cursors.SizeNWSE;
            else
                this.Cursor = Cursors.Default;
            if (e.Button == MouseButtons.Left)
            {
                if (e.X > mouseLeft)
                {
                    this.Height = this.Height + e.Y - mouseTop;
                    mouseTop = e.Y;
                }
                else
                {
                    this.Height = this.Height - (e.Y - mouseTop) * -1;
                    mouseTop = e.Y;
                }

                if (e.X < mouseLeft)
                {
                    this.Width = this.Width - (e.X - mouseLeft) * -1;
                    mouseLeft = e.X;
                }
                else
                {
                    this.Width = this.Width + e.X - mouseLeft;
                    mouseLeft = e.X;
                }
            }
        }

        private void FrmNotes_MouseDown(object sender, MouseEventArgs e)
        {
            mouseLeft = e.Location.X;
            mouseTop = e.Location.Y;
        }

        private void FrmNotes_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = FormWindowState.Normal;
            }
        }

        private void FrmNotes_Deactivate(object sender, EventArgs e)
        {
            if (this.changed)
            {
                this.annotation.Locked = miLockControl.Checked;
                this.annotation.FormSize = this.Size;
                this.annotation.Location = this.Location;

                if (this.annotation.Type == AnnotationType.Text)
                    this.annotation.Text = this.rtbText.Text;

                this.annotation.ModificationTime = DateTime.Now;
                Notes.SaveAnnotations();
            }
        }

        #endregion

        // Eventos de todos ContextMenu de edição.
        #region Events ContextMenuStrip Edition

        private void miAddImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";

            if (fd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Bitmap im = new Bitmap(fd.FileName);
                    Clipboard.SetDataObject(im);
                    DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
                    rtbText.Paste(myFormat);
                }
                catch
                {
                    throw new ArgumentException("Invalid image.");
                }
            }
        }

        private void miClear_Click(object sender, EventArgs e)
        {
            this.rtbText.Clear();
        }

        private void miImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif";
            fd.Multiselect = false;

            if (fd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    LoadImage(fd.FileName);
                }
                catch
                {
                    throw new ArgumentException("Invalid image.");
                }
            }
        }

        private void miFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Image Files|*.htm;*.html;*.mht;*.php";
            fd.Multiselect = false;

            if (fd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.wbNote.Navigate(fd.FileName);
                }
                catch
                {
                    throw new ArgumentException("Invalid page format.");
                }
            }
        }

        private void miWeb_Click(object sender, EventArgs e)
        {
            string url = InputBoxDialog.InputBox("Link to web site.", "MyNotes", "http://");

            if (!String.IsNullOrEmpty(url))
                try
                {
                    this.wbNote.Navigate(url);
                    this.annotation.Text = url;
                }
                catch
                {
                    throw new ArgumentException("Invalid url format.");
                }
        }

        #endregion

        // Eventos do menu de opções
        #region ContextMenusStrip General Options

        // Note Colors.
        private void miYellow_Click(object sender, EventArgs e)
        {
            miYellow.Checked = true;
            miGreen.Checked = false;
            miPurple.Checked = false;
            SwapColor(NoteColor.Yellow);
            changed = true;
        }

        private void miGreen_Click(object sender, EventArgs e)
        {
            miGreen.Checked = true;
            miYellow.Checked = false;
            miPurple.Checked = false;
            SwapColor(NoteColor.Green);
            changed = true;
        }

        private void miPurple_Click(object sender, EventArgs e)
        {
            miPurple.Checked = true;
            miGreen.Checked = false;
            miYellow.Checked = false;
            SwapColor(NoteColor.Purple);
            changed = true;
        }

        // Note Type.
        private void miTextType_Click(object sender, EventArgs e)
        {
            miTextType.Checked = true;
            miImageType.Checked = false;
            miWebType.Checked = false;
            AddRichText();
            this.annotation.Type = AnnotationType.Text;
            this.changed = true;
        }

        private void miImageType_Click(object sender, EventArgs e)
        {
            miImageType.Checked = true;
            miTextType.Checked = false;
            miWebType.Checked = false;
            AddImageBox();
            this.annotation.Type = AnnotationType.Image;
            this.changed = true;
        }

        private void miWebType_Click(object sender, EventArgs e)
        {
            miWebType.Checked = true;
            miImageType.Checked = false;
            miTextType.Checked = false;
            AddWebBrowser();
            this.annotation.Type = AnnotationType.Web;
            this.changed = true;
        }

        // Lock annotation.
        private void miLockControl_Click(object sender, EventArgs e)
        {
            if (!this.annotation.Locked)
            {
                this.miLockControl.Checked = true;
                this.annotation.Locked = true;
                if (this.pnText.Controls[0] is RichTextBox)
                    ((RichTextBox)this.pnText.Controls[0]).ReadOnly = true;
            }
            else
            {
                this.miLockControl.Checked = false;
                this.annotation.Locked = false;
                if (this.pnText.Controls[0] is RichTextBox)
                    ((RichTextBox)this.pnText.Controls[0]).ReadOnly = false;
            }
            changed = true;
        }

        #endregion
    }
}
